package cloud.daodao.storm.common.util.security;

import org.bouncycastle.crypto.digests.SM3Digest;

import java.nio.charset.StandardCharsets;
import java.util.HexFormat;

public class Sm3Util {

    public static String hash(String origin) {
        if (null == origin || origin.isEmpty()) {
            return origin;
        }
        byte[] bytes = origin.getBytes(StandardCharsets.UTF_8);
        SM3Digest digest = new SM3Digest();
        digest.update(bytes, 0, bytes.length);
        byte[] hashBytes = new byte[digest.getDigestSize()];
        digest.doFinal(hashBytes, 0);
        String hash = HexFormat.of().withUpperCase().formatHex(hashBytes);
        assert hash != null;
        return hash.toUpperCase();
    }

}
